unit ec_setswitches;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
  StdCtrls, EditBtn, Spin, ec_base;

type

  { TECSetSwitchesFrame }

  TECSetSwitchesFrame = class(TECBaseFrame)
    NewValueGroupBox: TGroupBox;
    OnRadioButton: TRadioButton;
    OffRadioButton: TRadioButton;
    SingleSwitchEditButton: TEditButton;
    SwitchToSetGroupBox: TGroupBox;
    SwitchRangeToLabel: TLabel;
    SwitchRangeFromSpinEdit: TSpinEdit;
    SwitchRangeFromLabel: TLabel;
    SingleSwitchRadioButton: TRadioButton;
    SwitchRangeToSpinEdit: TSpinEdit;
    SwitchRangeRadioButton: TRadioButton;
    procedure SingleSwitchEditButtonButtonClick(Sender: TObject);
    procedure SingleSwitchEditButtonKeyPress(Sender: TObject; var Key: char);
    procedure SwitchRadioButtonChange(Sender: TObject);
  private
    SingleSwitchId: Integer;
    procedure ToggleEnabledState;
    procedure UpdateSingleSwitchName;
  public
    procedure InitNew; override;
    procedure InitExisting; override;
    procedure MakeResultingCommand; override;
  end;

var
  ECSetSwitchesFrame: TECSetSwitchesFrame;

implementation

{$R *.lfm}

uses
  namedindexselection, database, fpjson, constants;

{ TECSetSwitchesFrame }

procedure TECSetSwitchesFrame.SwitchRadioButtonChange(Sender: TObject);
begin
  ToggleEnabledState
end;

procedure TECSetSwitchesFrame.SingleSwitchEditButtonButtonClick(Sender: TObject);
begin
  SingleSwitchId := NamedIndexSelectionForm.SelectSwitch(SingleSwitchId, Db);
  UpdateSingleSwitchName
end;

procedure TECSetSwitchesFrame.SingleSwitchEditButtonKeyPress(Sender: TObject;
  var Key: char);
begin
  Key := #0;
  SingleSwitchEditButtonButtonClick(Sender);
end;

procedure TECSetSwitchesFrame.ToggleEnabledState;
begin
  SingleSwitchEditButton.Enabled := SingleSwitchRadioButton.Checked;
  SwitchRangeFromSpinEdit.Enabled := SwitchRangeRadioButton.Checked;
  SwitchRangeToSpinEdit.Enabled := SwitchRangeRadioButton.Checked;
end;

procedure TECSetSwitchesFrame.UpdateSingleSwitchName;
begin
  SingleSwitchEditButton.Text := Db.GetSwitchName(SingleSwitchId);
end;

procedure TECSetSwitchesFrame.InitNew;
begin
  SingleSwitchId := 1;
  UpdateSingleSwitchName;
  ToggleEnabledState
end;

procedure TECSetSwitchesFrame.InitExisting;
var
  Params: TJSONArray;
  StartId: Integer = 1;
  EndId: Integer = 1;
  IsOff: Boolean = False;
begin
  Params := GetFirstParams;

  if (Params.Count >= 1) and (Params[0].JSONType = jtNumber) then
     StartId := Params.Integers[0];

  if (Params.Count >= 2) and (Params[1].JSONType = jtNumber) then
     EndId := Params.Integers[1];

  if (Params.Count >= 3) and (Params[2].JSONType = jtBoolean) then
     IsOff := Params.Booleans[2];


  SingleSwitchId := StartId;
  UpdateSingleSwitchName;
  SwitchRangeFromSpinEdit.Value := StartId;
  SwitchRangeToSpinEdit.Value := EndId;

  if StartId = EndId then begin
    SwitchRangeRadioButton.Checked := False;
    SingleSwitchRadioButton.Checked := True;
  end else begin
    SingleSwitchRadioButton.Checked := False;
    SwitchRangeRadioButton.Checked := True;
  end;
  ToggleEnabledState;

  if IsOff then begin
    OnRadioButton.Checked := False;
    OffRadioButton.Checked := True
  end else begin
    OnRadioButton.Checked := False;
    OnRadioButton.Checked := True
  end;
end;

procedure TECSetSwitchesFrame.MakeResultingCommand;
var
  StartId, EndId: Integer;
begin
  if SingleSwitchRadioButton.Checked then begin
    StartId := SingleSwitchId;
    EndId := SingleSwitchId;
  end else begin
    StartId := SwitchRangeFromSpinEdit.Value;
    EndId := SwitchRangeToSpinEdit.Value;
  end;

  ResultingCommand := TJSONArray.Create([
    TJSONObject.Create([
      'code', SET_SWITCHES_EC_CODE,
      'indent', Indent,
      'parameters', TJSONArray.Create([
                     StartId,
                     EndId,
                     OffRadioButton.Checked
                    ])
    ])
  ]);
end;

end.

